package com.ruoyi.system;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.api.vo.TrackBean;
import com.ruoyi.api.vo.VehicleTrack;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.result.ResultView;
import com.ruoyi.system.domain.SysAssetsVehicle;
import com.ruoyi.system.service.ISysAssetsVehicleService;
import com.ruoyi.util.HttpClientUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 资产Controller
 * 
 * @author ruoyi
 * @date 2020-07-07
 */
@Controller
@RequestMapping("/system/vehicle")
public class SysAssetsVehicleController extends BaseController
{
    private String prefix = "system/vehicle";
    @Value("${wspaas.wsVehicleUrl}")
    private String vehicleUrl;

    @Autowired
    private ISysAssetsVehicleService sysAssetsVehicleService;

    @RequiresPermissions("system:vehicle:view")
    @GetMapping()
    public String vehicle()
    {
        return prefix + "/vehicle";
    }

    @PostMapping("/info")
    @ResponseBody
    public ResultView getVehicleInfo(@RequestBody JSONObject o){
        if(null==o||null==o.get("carNo")){
            return ResultView.error(-1,"invalid param");
        }
        return ResultView.ok(sysAssetsVehicleService.selectSysAssetsVehicleByPlateNo(o.getString("carNo")));
    }

    /**
     * 查询资产列表
     */
    @RequiresPermissions("system:vehicle:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysAssetsVehicle sysAssetsVehicle)
    {
        startPage();
        return getDataTable(sysAssetsVehicleService.selectSysAssetsVehicleList(sysAssetsVehicle));
    }

    /**
     * 导出资产列表
     */
    @RequiresPermissions("system:vehicle:export")
    @Log(title = "资产", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysAssetsVehicle sysAssetsVehicle)
    {
        List<SysAssetsVehicle> list = sysAssetsVehicleService.selectSysAssetsVehicleList(sysAssetsVehicle);
        ExcelUtil<SysAssetsVehicle> util = new ExcelUtil<SysAssetsVehicle>(SysAssetsVehicle.class);
        return util.exportExcel(list, "vehicle");
    }

    /**
     * 新增资产
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    @GetMapping("/gotobdmap/{maptype}")
    public String gotoMap(@PathVariable String maptype,  String carno,ModelMap mmap)
    {
        StringBuilder sb = new StringBuilder();
        sb.append(vehicleUrl).append("/KeChuangVehicle.asmx/GetVehicleTrackRT");
        JSONObject o=new JSONObject();
        o.put("carNo",carno);
        String s = HttpClientUtil.doPost(sb.toString(), o);
        if (!"-1".equalsIgnoreCase(s)) {
            JSONObject result = JSON.parseObject(s);
            if (null != result && 0 == result.getInteger("code")) {//成功
                if (null != result.getJSONArray("data")) {
                    List<VehicleTrack> vts = JSON.parseArray(result.getJSONArray("data").toJSONString(), VehicleTrack.class);
                    if(null!=vts&&!vts.isEmpty()){
                        VehicleTrack v=vts.get(0);
                        sb.setLength(0);
                        sb.append("①.车辆:").append(v.getCarNo()).append("(").append(v.getCarType()).append(")<br/>").append("②.时间:").append(v.getReportDate()).append("<br/>");
                        List<TrackBean> track = v.getTrack();
                        if(null!=track&&!track.isEmpty()){
                            TrackBean tb=track.get(0);
                            sb.append("③.时速:").append(tb.getSpeed()).append("km/h");
                            mmap.put("msg",sb.toString());
                            mmap.put("lat",tb.getLatitude());
                            mmap.put("lng",tb.getLongitude());
                            return "system/vehicle/"+maptype;
                        }
                    }
                }
            }
        }
        mmap.put("lng",114.224693);
        mmap.put("lat",22.72769);
        mmap.put("msg",carno+"车辆没有信号,请联系相关人员");
        return prefix + "/bdmap_bounce";
    }

    /**
     * 新增保存资产
     */
    @RequiresPermissions("system:vehicle:add")
    @Log(title = "资产", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysAssetsVehicle sysAssetsVehicle)
    {
        return toAjax(sysAssetsVehicleService.insertSysAssetsVehicle(sysAssetsVehicle));
    }

    @PostMapping("/multiadd")
    @ResponseBody
    public String multiSave(@RequestBody List<SysAssetsVehicle> list)
    {
        StringBuilder sb=new StringBuilder();
        if(null!=list&&!list.isEmpty()){
            list.forEach(c->{
               int i= sysAssetsVehicleService.insertSysAssetsVehicle(c);
               sb.append(i);
            });
        }
        return sb.toString();
    }

    /**
     * 修改资产
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        SysAssetsVehicle sysAssetsVehicle = sysAssetsVehicleService.selectSysAssetsVehicleById(id);
        mmap.put("sysAssetsVehicle", sysAssetsVehicle);
        return prefix + "/edit";
    }

    /**
     * 修改保存资产
     */
    @RequiresPermissions("system:vehicle:edit")
    @Log(title = "资产", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysAssetsVehicle sysAssetsVehicle)
    {
        return toAjax(sysAssetsVehicleService.updateSysAssetsVehicle(sysAssetsVehicle));
    }

    /**
     * 删除资产
     */
    @RequiresPermissions("system:vehicle:remove")
    @Log(title = "资产", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysAssetsVehicleService.deleteSysAssetsVehicleByIds(ids));
    }
}
